home *** CD-ROM | disk | FTP | other *** search
- ' This control shows how you can create a composite control
-
- Imports System.ComponentModel
- Imports System.Web.UI
- Imports System.Web.UI.WebControls
-
- <ToolboxData("<{0}:Multiplier runat=server></{0}:Multiplier>")> _
- Public Class Multiplier
- Inherits System.Web.UI.WebControls.WebControl
- Implements INamingContainer
-
- ' These variables hold
- Dim txtFirst As TextBox
- Dim txtSecond As TextBox
- Dim txtResult As TextBox
- Dim WithEvents btnEval As Button
-
- Sub New()
- MyBase.New()
- Me.Width = Unit.Pixel(200)
- End Sub
-
- ' this is the method to override to create all the
- ' constituent control
-
- Protected Overrides Sub CreateChildControls()
- 'Create all child controls .
- txtFirst = New TextBox()
- txtSecond = New TextBox()
- btnEval = New Button()
- txtResult = New TextBox()
- Dim lblAsterisk As New Label()
-
- ' Set their properties
- lblAsterisk.Text = " * "
- btnEval.Text = " = "
- txtResult.ReadOnly = True
-
- ' Establish correct width for text controls.
- AdjustControlWidth()
-
- ' Add to the controls collection.
- Controls.Add(txtFirst)
- Controls.Add(lblAsterisk)
- Controls.Add(txtSecond)
- Controls.Add(btnEval)
- Controls.Add(txtResult)
- End Sub
-
- ' This property returns the result of the multiplication
-
- <Browsable(False)> _
- Property Result() As Double
- Get
- ' ensure child control exist before accessing one of them
- EnsureChildControls()
- Try
- Return CDbl(txtResult.Text)
- Catch
- Return 0
- End Try
- End Get
- Set(ByVal Value As Double)
- ' ensure child control exist before accessing one of them
- EnsureChildControls()
- txtResult.Text = Value.ToString
- End Set
- End Property
-
- ' this method does the actual rendering
-
- Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
- ' Ensure the child controls exist and then render them.
- EnsureChildControls()
- RenderChildren(output)
- ' Adjust their width.
- AdjustControlWidth()
- End Sub
-
- ' Adjust controls' width.
-
- Private Sub AdjustControlWidth()
- ' Evaluate the space available for the three textboxes.
- Dim w As Unit = Unit.Pixel(CInt(Me.Width.Value - 50) \ 3)
- txtFirst.Width = w
- txtSecond.Width = w
- txtResult.Width = w
- End Sub
-
- ' this event fires when the user clicks on the Eval button
-
- Private Sub btnEval_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEval.Click
- ' ensure child control exist before accessing them
- EnsureChildControls()
- Try
- ' attempt the multiplication
- Dim res As Double = CDbl(txtFirst.Text) * CDbl(txtSecond.Text)
- ' assign a value if successful
- txtResult.Text = res.ToString
- Catch
- ' show an error message otherwise
- txtResult.Text = "# ERR #"
- End Try
- End Sub
-
- End Class
-